function addSysArea(){
	$.fn.tree.getSelect();
	var id=$.fn.tree.tmpid;
	var level=getLevel();
	var parentId=getParentId();
	if(id==undefined || id=="" || id=="0" || id==0){
		easyDialog.open({
			container:{
				header:"温馨提示",
				content:"请选择要操作的记录！"
			},
			autoClose : 2000
		});
		return;
	}
	if(level==6){
		easyDialog.open({
			container:{
				header:"温馨提示",
				content:"该级区域不能添加！"
			},
			autoClose : 2000
		});
		return;
	}
	var token=localStorage.getItem("token");
	$('#systemOutTree').remove();
	$.ajax({
		url:server_url+'sysAreaManager/initSysAddPage.do?ran='+Math.random()+'&token='+token,
		type:'get',
		data: {id:id,level:level,parentId:parentId},
		success:function(data){
			var bb=transformTozTreeFormat(JSON.stringify(data.data.tree));
			var html='<div class="z_rightMidden">'
				+'<div class="z_title">首页>系统区域管理>添加区域</div>'
				+'<div class="l_system">'
				+'<ul>'
				+'<li class="clearfix">'
				+'<span>区域名称</span>'
				+'<input type="text" id="areaName" maxlength="30" onkeyup="checkAreaName();"/>'
				+'<em>*</em>'+'<em id="tishi" style="margin:0px 0 0 0px;"></em>'
				+'</li>'
				+'<li class="clearfix">'
				+'<span>地区</span>'
				+'<div class="l_adress">';
			if(level==1||level==2||level==3||level==4||level==5){
				//只选中中国下
				html+='<span>中国</span>';
			}
			if(level==2||level==3||level==4||level==5){
				//选择省下
				html+='<em>></em><span>'+data.data.shengName+'</span>';
			}
			if(level==3||level==4||level==5){
				//选择市下
				html+='<em>></em><span>'+data.data.shiName+'</span>';
			}
			if(level==4||level==5){
				//选择县下
				html+='<em>></em><span>'+data.data.xianName+'</span>';
			}
			if(level==5){
				//选择镇下
				html+='<em>></em><span>'+data.data.zhenName+'</span>';
			}
			html+='</div>'
				+'</li>'
				+'<li class="clearfix">'
				+'<span>区域代码</span>'
				+'<div>';
			if(level==1||level==2||level==3){
				html+='<input type="text" id="areaCode" maxlength="6" onkeyup="this.value=this.value.replace(/\\D/g,\'\')" onafterpaste="this.value=this.value.replace(/\\D/g,\'\')"/>'
			}
			if(level==4||level==5){
				html+='<input type="text" id="areaCode" maxlength="12" onkeyup="this.value=this.value.replace(/\\D/g,\'\')" onafterpaste="this.value=this.value.replace(/\\D/g,\'\')"/>'
			}
			html+='</div>'
				+'</li>'
				+'<li class="clearfix">'
				+'<span>备注</span>'
				+'<textarea name="" id="areaDesc" cols="30" rows="10" maxlength="100"></textarea>'
				+'</li>'
				+'</ul>'
				+'<input type="hidden" id="parentId" value="'+data.data.parentId+'"/>'
				+'<input type="hidden" id="id" value="'+data.data.id+'"/>'
				+'<input type="hidden" id="level" value="'+data.data.level+'"/>'
				+'</div>'
				+'<div class="z_zone"style="margin-left: 500px; margin-top: -40px;position: relative">'
				+'</div>'
				+'<a href="javascript:addSysSubmit();" class="z_sure"  style="margin-left: 500px;margin-top: 30px;position: absolute">确定</a>'
				+'<a href="javascript:returnSysList2();" class="z_goback" style="margin-top: 30px;margin-left:600px;position: absolute">返回</a>'
				+'</div>';
			$('#systemTreeAdd').html(html);
			$('.z_country .z_address1').remove();
			//清空数据源*
			$.fn.tree.html_str="";
			$.fn.tree.tmpid="";
			$.fn.tree.init({
				nodes:(JSON.stringify(bb))
			});
		}
	});
}
function getId(){
	return $.fn.tree.getSelect();
};
function getParentId(){
	return $.fn.tree.obj_tu.parentId;
};
function getLevel(){
	return $.fn.tree.obj_tu.level;
};



function addSysSubmit(){
	var id=$("#id").val();
	$.fn.tree.getSelect();
	var adminAreaId=$.fn.tree.tmpid;
	console.log("关联区域："+adminAreaId);
	var areaName=$("#areaName").val();
	var areaCode=$("#areaCode").val();
	var areaDesc=$("#areaDesc").val();
	var parentId=$("#parentId").val();
	var level=$("#level").val();
	var username=localStorage.getItem("username");
	var token=localStorage.getItem("token");
	var reg = /^([0-9.]+)$/;
	if(areaName=="" || areaName==undefined){
		$("#tishi").text("该值不能为空");
		return;
	}
	if(areaCode!=null && areaCode!="" && areaCode !=undefined){
		if(reg.test(areaCode) != true){
			easyDialog.open({
				container:{
					header:"温馨提示",
					content:"区域代码只能为数字！"
				},
				autoClose : 2000
			});
			return;
		}
		isExistSysAreaCode(null,areaCode);
	}
	var isExist = $("#isExist").val();
	if(isExist=="1"){
		easyDialog.open({
			container:{
				header:"温馨提示",
				content:"区域代码不能相同！"
			},
			autoClose : 2000
		});
		return;
	}
	$.ajax({
		url:server_url+'sysAreaManager/addSystemTree.do?ran='+Math.random()+'&token='+token,
		type:'post',
		async:false,
		data: {id:id,parentId:parentId,adminAreaId:adminAreaId,level:level,areaCode:areaCode,areaDesc:areaDesc,areaName:areaName,username:username},
		success:function(data){
//			window.location="http://localhost:8080/TianlianSaasMgrWeb/showSysAreaPage.do";
			back2();
		}
	});
}
function returnSysList2(){
//	window.location="http://localhost:8080/TianlianSaasMgrWeb/showSysAreaPage.do";
	back2();
}
function back2(){
	tl_config.loadToWorkArea("showSysAreaPage.do?ran="+Math.random());
}
function transformTozTreeFormat(sNodes) {
	sNodes=$.parseJSON(sNodes);
	var i,l,
		key = "areaId",
		parentKey = "parentId",
		childKey = "nodes";
	if (!key || key=="" || !sNodes) return [];
	var r = [];
	var tmpMap = [];
	for (i=0, l=sNodes.length; i<l; i++) {
		tmpMap[sNodes[i][key]] = sNodes[i];
	}
	for (i=0, l=sNodes.length; i<l; i++) {
		if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
			if (!tmpMap[sNodes[i][parentKey]][childKey])
				tmpMap[sNodes[i][parentKey]][childKey] = [];
			tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
		} else {
			r.push(sNodes[i]);
		}
	}
	return r;
}

function isExistSysAreaCode(id,areaCode){
	$.ajax({
		url:server_url+'sysAreaManager/isExistSysAreaCode.do?ran='+Math.random()+'&token='+token,
		type:'post',
		async:false,
		data: {id:id,areaCode:areaCode},
		success:function(data){
			if(data.retCode == '1003') {
				localStorage.clear();
				window.location.href = "login.do";
				return;
			}
			if(data.retCode == '0002'){
				$("#isExist").val("1");
			}
			if(data.retCode == '0000'){
				$("#isExist").val("0");
			}
		}
	});
}

function checkAreaName(){
	var areaName=$("#areaName").val();
	if(areaName=="" || areaName==undefined){
		$("#tishi").text("该值不能为空");
	}else{
		$("#tishi").text("");
	}
}
